<?php

namespace App\Modules\Health\Http\Controllers\Api;

use App\Constants\AdvConfigConstant;
use App\Constants\CollectTypeConstant;
use App\Constants\MiniProgramConstant;
use App\Http\Controllers\Api\Controller;
use App\Models\Collect;
use App\Modules\Health\Models\HealthDiseaseDiscern;
use App\Modules\Health\Models\HealthDiseaseDiscernClass;
use Illuminate\Http\Request;

class DiseaseDiscernController extends Controller
{
    public function index(Request $request)
    {
        $class = HealthDiseaseDiscernClass::select(['id', 'name', 'icon'])->where('is_show', 1)->get();
        $config = $this->config();

        return $this->_success(compact('class', 'config'));
    }

    public function list(Request $request)
    {
        $classId = $request->input('class_id');
        $keyword = $request->input('keyword');

        $list = HealthDiseaseDiscern::select(['id', 'name', 'img'])
            ->where('is_show', 1)
            ->when($classId, function ($q) use ($classId) {
                return $q->where('class_id', $classId);
            })
            ->when($keyword, function ($q) use ($keyword) {
                return $q->where('name', 'like', '%' . $keyword . '%');
            })
            ->paginate();

        return $this->_success(['list' => $list->items(), 'config' => $this->config()]);
    }

    public function info(int $id)
    {
        $info = HealthDiseaseDiscern::find($id);

        if (!$info || !$info->is_show) {
            return $this->error('信息不存在');
        }

        $memberId = auth('api')->user()->id ?? 0;

        $isCollect = 0;

        if ($memberId) {
            $exists = Collect::where([
                'member_id'   => $memberId,
                'belong_type' => CollectTypeConstant::TYPE_BY_DISEASE_DISCERN,
                'belong_id'   => $id
            ])->exists();

            $isCollect = $exists ? 1 : 0;
        }

        $info->is_collect = $isCollect;

        $info->load('class:id,name,icon');

        $config = $this->config();

        return $this->_success(compact('info', 'config'));
    }


    public function collect(int $id)
    {
        $memberId = auth('api')->user()->id ?? 0;

        if (!$memberId) return $this->error('请先登录', [], 40001, 401);

        $discern = HealthDiseaseDiscern::find($id);
        if (!$discern) return $this->error('信息不存在');

        $info = Collect::where([
            'member_id'   => $memberId,
            'belong_type' => CollectTypeConstant::TYPE_BY_DISEASE_DISCERN,
            'belong_id'   => $id
        ])->first();

        if ($info) {
            $info->delete();
        } else {
            Collect::create([
                'member_id'   => $memberId,
                'belong_type' => CollectTypeConstant::TYPE_BY_DISEASE_DISCERN,
                'belong_id'   => $id
            ]);
        }

        return $this->success('操作成功');
    }

    protected function config(): array
    {
        $settings = getMiniSettings($this->getMiniType(), MiniProgramConstant::GROUP);

        return [
            'switch'      => $settings[MiniProgramConstant::DISEASE_DISCERN_DIY_FORM_SWITCH] ?? 0,
            'diy_form_id' => $settings[MiniProgramConstant::DISEASE_DISCERN_DIY_FORM_ID] ?? 0,
        ];
    }
}
